package com.wyzz.hr.bo.impl;

import java.util.Collection;
import java.util.List;
import org.hibernate.criterion.*;

import com.wyzz.hr.bo.IInmatchModelBO;
import com.wyzz.hr.dao.IInmatchModelDAO;
import com.wyzz.hr.pojo.InmatchModel;
import com.wyzz.hr.pojo.Iodef;

public class InmatchModelBOImpl implements IInmatchModelBO {

	public InmatchModelBOImpl() {
	}

	public IInmatchModelDAO getDao() {
		return dao;
	}

	public void setDao(IInmatchModelDAO dao) {
		this.dao = dao;
	}

	public InmatchModel loadObject(String id, String fetch[]) {
		return (InmatchModel) dao.loadObject(InmatchModel.class, id, fetch,
				new boolean[0]);
	}

	public boolean saveOrUpdate(InmatchModel imm) {
		dao.saveOrupdate(imm);
		if (imm.getImList() != null && imm.getImList().size() > 0)
			dao.saveOrupdate(imm.getImList());
		return true;
	}

	public boolean saveOrUpdate(Collection imms) {
		dao.saveOrupdate(imms);
		return true;
	}

	public boolean deleteInmatchModel(InmatchModel inmatchModel) {
		if (inmatchModel.getImList() != null
				&& inmatchModel.getImList().size() > 0)
			dao.deleteCollection(inmatchModel.getImList());
		dao.deleteObject(inmatchModel);
		return true;
	}

	public InmatchModel loadDefaultObject(Iodef iodef) {
		DetachedCriteria detachedCriteria = DetachedCriteria
				.forClass(InmatchModel.class);
		detachedCriteria.add(Restrictions.eq(InmatchModel.PROP_IMM_IO, iodef));
		detachedCriteria.add(Restrictions.eq(InmatchModel.PROP_IMM_DEFAULT,
				Integer.valueOf(1)));
		return (InmatchModel) dao.findByCriteria(detachedCriteria, 1, 1).get(0);
	}

	public InmatchModel getObject(Iodef iodef, String ommId) {
		InmatchModel result = loadObject(ommId, null);
		if (result != null
				&& result.getImmIo().getIoId().compareTo(iodef.getIoId()) == 0)
			return result;
		else
			return null;
	}

	public List getListByIodef(Iodef iodef) {
		DetachedCriteria detachedCriteria = DetachedCriteria
				.forClass(InmatchModel.class);
		detachedCriteria.add(Restrictions.eq(InmatchModel.PROP_IMM_IO, iodef));
		detachedCriteria.addOrder(Order.asc(InmatchModel.PROP_IMM_NAME));
		List result = dao.findByCriteria(detachedCriteria);
		System.out.println(result.size()+"............."+iodef.getIoName());
		return result;
	}

	private IInmatchModelDAO dao;
}
